import 'dart:io';
import 'package:flutter/material.dart';
import '../../ui.dart';
import 'model/app_file_desc.dart';

class AppTextFileViewPage extends StatefulWidget {
  final AppFileDesc fileDesc;
  const AppTextFileViewPage({super.key, required this.fileDesc});

  @override
  State<AppTextFileViewPage> createState() => _AppTextFileViewPageState();
}

class _AppTextFileViewPageState extends State<AppTextFileViewPage> {
  AppFileDesc get fileDesc => widget.fileDesc;

  String body = "";

  loadFile() async {
    final file = File(fileDesc.path);
    body = await file.readAsString();
    setState(() {});
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    loadFile();
  }

  @override
  Widget build(BuildContext context) {
    return MyScaffold(
      title: fileDesc.name,
      padding: const EdgeInsets.all(16),
      body: body.isNotEmpty
          ? SingleChildScrollView(
              child: SelectableText(
                body,
                style: TextStyle(
                    fontSize: 17,
                    fontWeight: FontWeight.normal,
                    color: JmColors.textColor),
              ),
            )
          : Center(child: MyText.sub("暂无内容")),
    );
  }
}
